package com.example.searchservice;

import com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.http.MediaType;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;

@EnableFeignClients(basePackages = {"com.example"})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})
@MapperScan("com.example.**.mapper")
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.example","com.example.cloudcommon"})
@EnableElasticsearchRepositories(basePackages = "com.example.searchservice.repository")
//@ServletComponentScan
public class SearchServiceApplication {

    @Bean
    public WebExceptionHandler jsonExceptionHandler() {
        return (exchange, ex) -> {
            exchange.getResponse().getHeaders()
                    .setContentType(MediaType.APPLICATION_JSON);
            return exchange.getResponse()
                    .writeWith(Mono.just(exchange.getResponse()
                            .bufferFactory().wrap("{\"error\":\"Invalid request\"}".getBytes())));
        };
    }

    public static void main(String[] args) {
        SpringApplication.run(SearchServiceApplication.class, args);
    }
}